//
//  SXTabBarController.m
//  SXDataManager-SXDataManager
//
//  Created by wangshuixin on 2019/1/13.
//

#import "SXTabBarController.h"
#import <objc/runtime.h>
#import "SXRouter.h"

@interface SXTabBarController ()

@property (assign, nonatomic) NSInteger index;

@end

@implementation SXTabBarController

+ (instancetype)sharedInstance {
    static SXTabBarController *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *array = @[@{@"vc":@"SXOneViewController", @"itemTitle":@"消息", @"icon_normal":@"menu-message-normal", @"icon_selected":@"menu-message-down"}, @{@"vc":@"SXTwoViewController", @"itemTitle":@"联系人", @"icon_normal":@"menu-contact-normal", @"icon_selected":@"menu-contact-down"}, @{@"vc":@"SXThreeViewController", @"itemTitle":@"更多", @"icon_normal":@"menu-more-normal", @"icon_selected":@"menu-more-down"}, @{@"vc":@"SXOneViewController", @"itemTitle":@"无题", @"icon_normal":@"menu-message-normal", @"icon_selected":@"menu-message-down"}];
    [SXTabBarController addChildViewControllers:array OnTabBarController:self];
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
+(void)addChildViewControllers:(NSArray *)controllers OnTabBarController:(UITabBarController *)tabBarController {
    for (NSDictionary *controlDict in controllers) {
        UIViewController *childController = [[NSClassFromString(controlDict[@"vc"]) alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childController];
        nav.navigationBarHidden = YES;
        [tabBarController addChildViewController:nav];
        UITabBarItem *tabBarItem = [[UITabBarItem alloc] init];
        tabBarItem.title = controlDict[@"itemTitle"];
        tabBarItem.image = [[NSClassFromString(@"SXGetBundleData") performSelector:@selector(SXImageName:) withObject:controlDict[@"icon_normal"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        tabBarItem.selectedImage = [[NSClassFromString(@"SXGetBundleData") performSelector:@selector(SXImageName:) withObject:controlDict[@"icon_selected"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        nav.tabBarItem = tabBarItem;
    }
    // 设置tabBarItem的文字样式
    UIColor *tintColor = [UIColor colorWithRed:18.0/255 green:183.0/255 blue:245.0/255 alpha:1.0];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:tintColor, NSForegroundColorAttributeName,nil,nil] forState:UIControlStateSelected];
    [SXTabBarController configNavigationBar:tabBarController atIndex:0];
}

// 设置tabBarController的navigationController
+ (void)configNavigationBar:(UITabBarController *)tabBarController atIndex:(NSInteger)index {
    [SXTabBarController resetNavigationBarConfig:tabBarController];
    switch (index) {
        case 0:
        {
            tabBarController.title = @"主题1";
            UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
            leftButton.frame = CGRectMake(0, 0, 60, 30);
            leftButton.backgroundColor = [UIColor redColor];
            [leftButton addTarget:self action:@selector(respondToBackEvent) forControlEvents:UIControlEventTouchUpInside];
            [leftButton setTitle:@"返回" forState:UIControlStateNormal];
            UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
            tabBarController.navigationItem.leftBarButtonItem = leftItem;
        }
            break;
        case 1:
        {
            tabBarController.title = @"主题2";
            UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
            leftButton.frame = CGRectMake(0, 0, 60, 30);
            leftButton.backgroundColor = [UIColor redColor];
            [leftButton addTarget:self action:@selector(respondToBackEvent) forControlEvents:UIControlEventTouchUpInside];
            [leftButton setTitle:@"返回" forState:UIControlStateNormal];
            UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
            tabBarController.navigationItem.rightBarButtonItem = leftItem;
        }
            break;
        case 2:
        {
            tabBarController.title = @"主题3";
        }
            break;
        case 3:
        {
            
        }
            break;
        case 4:
        {
            
        }
            break;
        default:
            break;
    }
    
}

// 清空导航栏的配置
+ (void)resetNavigationBarConfig:(UITabBarController *)tabBarController {
    tabBarController.title = nil;
    tabBarController.navigationItem.leftBarButtonItem = nil;
    tabBarController.navigationItem.rightBarButtonItem = nil;
    tabBarController.navigationItem.rightBarButtonItems = nil;
}

// 点击tabbarItem自动调用
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSInteger index = [self.tabBar.items indexOfObject:item];
    if (index != _index) {
        [self respondToAnimationEventOnTabBar:tabBar atIndex:index];
        [self respondToClickEventOnTabBarItem:item atIndex:index];
        [SXTabBarController configNavigationBar:self atIndex:index];
        _index = index;
    }
}

// item点击响应事件
- (void)respondToClickEventOnTabBarItem:(UITabBarItem *)tabBarItem atIndex:(NSInteger)index {
    [NSClassFromString(@"SXToast") performSelector:@selector(makeToastViewMaxArea:) withObject:@{@"maxWidth":@(100), @"maxHeight":@(300)}];
    NSLog(@"点中第%ld个item", index);
    tabBarItem.badgeValue = nil;
    if ([tabBarItem.title isEqualToString:@"更多"]) {
        NSLog(@"点击中了更多item！");
        [NSClassFromString(@"SXToast") performSelector:@selector(showToastByRule:) withObject:@{@"message":@"测试啊", @"duration":@(1.5), @"location":@"center"}];
//        [NSClassFromString(@"SXToast") performSelector:@selector(showToastByRule:) withObject:@{@"message":@"测试啊", @"duration":@(1.5)}];
    } else if ([tabBarItem.title isEqualToString:@"消息"]) {
        [NSClassFromString(@"SXToast") performSelector:@selector(showToastByRule:) withObject:@{@"message":@"测试啊dddsdjhi师大暨大第四大街", @"duration":@(2), @"location":@"top"}];
    } else if ([tabBarItem.title isEqualToString:@"联系人"]) {
        [NSClassFromString(@"SXToast") performSelector:@selector(showToastByRule:) withObject:@{@"message":@"测试啊dddsdjhi师大底部测试底部底部底部!!!!测试啊dddsdjhi师大底部测试底部底部底部测试啊dddsdjhi师大底部测试底部底部底部!!!!!!!!测试啊dddsdjhi师大底部测试底部底部底部!!!!测试啊dddsdjhi师大底部测试底部底部底部测试啊dddsdjhi师大底部测试底部底部底部!!!!!!!!测试啊dddsdjhi师大底部测试底部底部底部!!!!测试啊dddsdjhi师大底部测试底部底部底部测试啊dddsdjhi师大底部测试底部底部底部!!!!!!!!测试啊dddsdjhi师大底部测试底部底部底部!!!!测试啊dddsdjhi师大底部测试底部底部底部测试啊dddsdjhi师大底部测试底部底部底部!!!!!!!!", @"duration":@(2.4), @"location":@"bottom"}];
    }
}
#pragma clang diagnostic pop

// tabBar点击特效
- (void)respondToAnimationEventOnTabBar:(UITabBar *)tabBar atIndex:(NSInteger)index {
    // 设置动画部分
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    /**
     CABasicAnimation类的使用方式就是基本的关键帧动画。
     
     所谓关键帧动画，就是将Layer的属性作为KeyPath来注册，指定动画的起始帧和结束帧，然后自动计算和实现中间的过渡动画的一种动画方式。
     */
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.2;
    pulse.repeatCount= 1;
    pulse.autoreverses= YES;
    pulse.fromValue= [NSNumber numberWithFloat:0.95];
    pulse.toValue= [NSNumber numberWithFloat:1.1];
    [[tabbarbuttonArray[index] layer] addAnimation:pulse forKey:nil];
}

// 点击返回按钮
+ (void)respondToBackEvent {
//    [SXRouter openURL:@"mgj://detail?id=404&token=qasqw"];
    [SXRouter openURL:@"mgj://detail?id=404&token=qasqw" withUserInfo:@{@"info":@"you know!"} completion:^(id  _Nonnull result) {
        NSLog(@"回调！");
    }];
}

@end
